//
//  ShelfViewController.m
//  ZYTS-iPhone
//
//  Created by yuzhou on 12-12-7.
//  Copyright (c) 2012年 wzyk. All rights reserved.
//

#import "ShelfViewController.h"
#import "PlayerBarView.h"

@interface ShelfViewController ()

@end

@implementation ShelfViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updatePlayerBar) name:kPlayerStateDidChange object:nil];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    completeEditButton.userInteractionEnabled = NO;
    completeEditButton.alpha = 0;
    selectAllButton.alpha = 0;
    deleteButton.alpha = 0;
    [self selectButtonTag:kShelfButtonDownloading];
    [self updatePlayerBar];
}

- (void)updateDiaplay
{
    if (downloadedViewController) {
        [downloadedViewController updateDisplay];
    }
    if (downloadingViewController) {
        [downloadingViewController updateDisplay];
    }
    if (playHistoryViewController) {
        [playHistoryViewController updateDisplay];
    }
}

- (void)updatePlayerBar {
    // 判断是否正在播放
    if ([CollectionData hasContent:kCollection_playing]) {
        if (playerBarView || contentView == nil) {
            return;
        }
        
        playerBarView = [[[NSBundle mainBundle] loadNibNamed:@"PlayerBarView" owner:self options:nil] objectAtIndex:0];
        CGRect frameTable = contentView.frame;
        CGRect frameBar = playerBarView.frame;
        frameBar.origin.x = frameTable.origin.x;
        frameBar.origin.y = frameTable.origin.y;
        playerBarView.frame = frameBar;
        [self.view addSubview:playerBarView];
        
        frameTable.origin.y += PlayerBarHeight;
        frameTable.size.height -= PlayerBarHeight;
        contentView.frame = frameTable;
    }
    else if (playerBarView) {
        [playerBarView removeFromSuperview];
        playerBarView = nil;
        
        CGRect frameTable = contentView.frame;
        frameTable.origin.y -= PlayerBarHeight;
        frameTable.size.height += PlayerBarHeight;
        contentView.frame = frameTable;
    }
}

- (IBAction)shelfButtonAction:(UIButton *)sender
{
    [self selectButtonTag:sender.tag];
}

- (void)selectButtonTag:(int)tag
{
    for (UIButton *button in tabsView.subviews) {
        if ([button isKindOfClass:[UIButton class]]) {
            button.selected = (button.tag == tag);
            button.userInteractionEnabled = (button.tag != tag);
        }
    }
    
    [self clearContentView];
    
    switch (tag) {
        case kShelfButtonDownloaded:
            if (downloadedViewController == nil) {
                downloadedViewController = [[DownloadedViewController alloc] initWithNibName:@"DownloadedViewController" bundle:nil];
                downloadedViewController.view.frame = contentView.bounds;
            }
            [contentView addSubview:downloadedViewController.view];
            [downloadedViewController updateDisplay];
            break;
        case kShelfButtonDownloading:
            if (downloadingViewController == nil) {
                downloadingViewController = [[DownloadingViewController alloc] initWithNibName:@"DownloadingViewController" bundle:nil];
                downloadingViewController.view.frame = contentView.bounds;
            }
            [contentView addSubview:downloadingViewController.view];
            [downloadingViewController updateDisplay];
            break;
        case kShelfButtonPlayrecord:
            if (playHistoryViewController == nil) {
                playHistoryViewController = [[PlayHistoryViewController alloc] initWithNibName:@"PlayHistoryViewController" bundle:nil];
                playHistoryViewController.view.frame = contentView.bounds;
            }
            [contentView addSubview:playHistoryViewController.view];
            [playHistoryViewController updateDisplay];
            break;
        default:
            break;
    }
    currentTab = tag;
}

- (void)clearContentView
{
    for (UIView *view in contentView.subviews) {
        [view removeFromSuperview];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)beginEdit:(id)sender
{
    [UIView animateWithDuration:0.2 animations:^{
        tabsView.alpha = 0;
        beginEditButton.alpha = 0;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.3 animations:^{
            completeEditButton.alpha = 1;
            selectAllButton.alpha = 1;
            deleteButton.alpha = 1;
        } completion:^(BOOL finished) {
            beginEditButton.userInteractionEnabled = NO;
            completeEditButton.userInteractionEnabled = YES;
        }];
    }];
    switch (currentTab) {
        case kShelfButtonDownloaded:
            [downloadedViewController beginEdit];
            break;
        case kShelfButtonDownloading:
            [downloadingViewController beginEdit];
            break;
        case kShelfButtonPlayrecord:
            [playHistoryViewController beginEdit];
            break;
        default:
            break;
    }
}

- (IBAction)completeEdit:(id)sender
{
    [UIView animateWithDuration:0.2 animations:^{
        completeEditButton.alpha = 0;
        selectAllButton.alpha = 0;
        deleteButton.alpha = 0;
        
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.3 animations:^{
            tabsView.alpha = 1;
            beginEditButton.alpha = 1;
        } completion:^(BOOL finished) {
            beginEditButton.userInteractionEnabled = YES;
            completeEditButton.userInteractionEnabled = NO;
        }];
    }];
    switch (currentTab) {
        case kShelfButtonDownloaded:
            [downloadedViewController completeEdit];
            break;
        case kShelfButtonDownloading:
            [downloadingViewController completeEdit];
            break;
        case kShelfButtonPlayrecord:
            [playHistoryViewController completeEdit];
            break;
        default:
            break;
    }
}

- (IBAction)deleteSeleted:(id)sender
{
    switch (currentTab) {
        case kShelfButtonDownloaded:
            [downloadedViewController deleteSelected];
            break;
        case kShelfButtonDownloading:
            [downloadingViewController deleteSelected];
            break;
        case kShelfButtonPlayrecord:
            [playHistoryViewController deleteSelected];
            break;
        default:
            break;
    }
}

- (IBAction)selectAll:(id)sender
{
    switch (currentTab) {
        case kShelfButtonDownloaded:
            [downloadedViewController selectAll];
            break;
        case kShelfButtonDownloading:
            [downloadingViewController selectAll];
            break;
        case kShelfButtonPlayrecord:
            [playHistoryViewController selectAll];
            break;
        default:
            break;
    }
}

- (void)dealloc {
    [toolView release];
    [contentView release];
    [beginEditButton release];
    [completeEditButton release];
    [deleteButton release];
    [selectAllButton release];
    [tabsView release];
    [super dealloc];
}
- (void)viewDidUnload {
    [toolView release];
    toolView = nil;
    [contentView release];
    contentView = nil;
    playerBarView = nil;
    [beginEditButton release];
    beginEditButton = nil;
    [completeEditButton release];
    completeEditButton = nil;
    [deleteButton release];
    deleteButton = nil;
    [selectAllButton release];
    selectAllButton = nil;
    [tabsView release];
    tabsView = nil;
    [super viewDidUnload];
}

@end
